using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;

namespace Socket_CS
{
    class SocketState
    {
        int _BufferSize;
        public int BufferSize
        {
            get { return _BufferSize; }
            set
            {
                if (_BufferSize < value)
                {
                    _BufferSize = (value & 0x3FF) == 0 ? value : ((value >> 10) + 1) << 10;
                    _Buffer = new byte[_BufferSize];
                }
            }
        }
        byte[] _Buffer;
        public byte[] Buffer { get { return _Buffer; } }
        public int MessageLength;
        public string StringMessage
        {
            get { return Encoding.ASCII.GetString(_Buffer, 0, MessageLength); }
            set
            {
                BufferSize = Encoding.ASCII.GetByteCount(value);
                MessageLength = Encoding.ASCII.GetBytes(value, 0, value.Length, _Buffer, 0);
            }
        }
        public EndPoint RemoteEndPoint;

        public SocketState(EndPoint remoteEndPoint)
        {
            BufferSize = AsyncSocket.BufferSize;
            _Buffer = new byte[BufferSize];
            RemoteEndPoint = remoteEndPoint;
        }

        public SocketState() : this((EndPoint)(new IPEndPoint(IPAddress.Any, 0))) { }

        public SocketState(EndPoint remoteEndPoint, byte[] message, int messageLength)
        : this(remoteEndPoint)
        {
            BufferSize = messageLength;
            message.CopyTo(_Buffer, 0);
            MessageLength = messageLength;
        }

        public SocketState(EndPoint remoteEndPoint, byte[] message)
        : this(remoteEndPoint, message, message.Length) { }

        public SocketState(EndPoint remoteEndPoint, string stringMessage)
        : this(remoteEndPoint)
        {
            StringMessage = stringMessage;
        }
    }
}